<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>平均值计算</title>
  <style>
    body {
      margin: 0;
      padding: 0;
      background: url("img/蓝色粒子.jpg");
      background-size: cover;

      background-position: center;
      font-family: "微软雅黑";

    }

    .container {
      width: 450px;
      height: 45vh;
      margin: 50px auto;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      background-color: #fff;
      border: 1px solid white;
      box-shadow: 0 10px 10px rgb(87, 87, 250);
      border-radius: 10px;
    }

    input {
      width: 380px;
      height: 30px;
      border: 1px solid #ccc;
      border-radius: 5px;
      padding: 0 10px;
      text-align: center;
    }

    .container button {
      width: 400px;
      height: 30px;
      border: 1px solid #ccc;
      border-radius: 5px;
      padding: 0 10px;
      background-color: #5c86f9;
      box-shadow: 0 0 5px #ccc;
    }
  </style>
  <script>
    function calculate() {

      let n0 = Number(document.getElementById("inputAmount1").value);
      let n1 = Number(document.getElementById("inputAmount2").value);

      // console.log("n0=", typeof n0);
      // console.log("n1=", n1);
      let result = 0;
      if (n0 >= 0 && n1 >= 0) {
        // toFixed
        result = ((n0 + n1) / 2).toFixed(3);
        // console.log("result", result);
        document.getElementById("result").innerHTML = "计算结果：" + result;
      } else {
        alert("请按规则输入正整数，平均值不能为负数");
        document.getElementById("result").innerHTML = "平均值：0";
      }
    }
  </script>
</head>

<body>
  <div class="container">
    <h1>平均值计算</h1>
    <p>
      <input type="text" id="inputAmount1" placeholder="请输入第一个数值" />
    </p>
    <p>
      <input type="text" id="inputAmount2" placeholder="请输入第二个数值" />
    </p>
    <p><button onclick="calculate()">计算</button></p>
    <p id="result">初始值：0</p>
  </div>

</body>

</html>